package codetop;

import java.util.*;

public class LRU {
    public static void main(String[] args) {

    }
    static class  LRUCache{
        int capacity;
        int[][] cache;
        int time = 0;
        Map<Integer, Integer> map = new HashMap<>();
        public LRUCache(int capacity) {
            this.capacity = capacity;
            this.cache = new int[2][capacity];
            Arrays.fill(cache[0], -1);
        }

        public int get(int key) {
            if(map.containsKey(key)){
                for (int i = 0; i < capacity; i++) {
                    if(cache[0][i] == key){
                        cache[1][i] = ++time;
                        break;
                    }
                }
                System.out.println(map);
                return map.get(key);
            }
            System.out.println(map);
            return -1;
        }

        public void put(int key, int value) {
            if(map.containsKey(key)){
                map.replace(key,value);
                for (int i = 0; i < capacity; i++) {
                    if(cache[0][i] == key){
                        cache[1][i] = ++time;
                        break;
                    }
                }
            }
            else if(map.size() < capacity){
                map.put(key, value);
                for (int i = 0; i < capacity; i++) {
                    if(cache[0][i] == -1){
                        cache[0][i] = key;
                        cache[1][i] = ++time;
                        break;
                    }
                }
            }
            else{
                int min = Integer.MAX_VALUE;
                int k = -1;
                for (int i = 0; i < capacity; i++) {
                    if(cache[1][i] < min){
                        min = cache[1][i];
                        k = i;
                    }
                }
                System.out.println(k);
                map.remove(cache[0][k]);
                cache[0][k] = key;
                cache[1][k] = ++time;
                map.put(key, value);
            }
            System.out.println(map);
        }
    }
}
